package com.own.business.level.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.own.business.level.business.common.LevelModel;
import com.own.business.level.business.service.LevelConfigService;
import com.own.business.level.entity.bo.LevelConfigBo;
import com.own.business.level.entity.map.LevelConfigMapVo;
import com.own.business.level.entity.po.LevelConfig;
import com.own.business.level.entity.query.LevelConfigQuery;
import com.own.business.level.entity.vo.LevelConfigVo;
import com.own.business.level.mapper.LevelConfigMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * LevelConfigServiceImpl
 *
 * @author chenxueli
 * @date 2022-07-25 10:41:00
 */
@Slf4j
@Service
public class LevelConfigServiceImpl extends AbstractBaseService<
        LevelConfig,
        LevelConfigBo,
        LevelConfigVo,
        LevelConfigMapVo,
        LevelConfigQuery,
        LevelConfigMapper
        > implements LevelConfigService {

    /**
     * 根据经验用户获取下一级的信息
     *
     * @param exp 经验数量
     * @return 下一级的信息
     */
    @Override
    public LevelConfigVo next(Integer exp) {
        var wrapper = lambdaQueryWrapper().gt(LevelConfig::getExp, exp).orderByAsc(LevelConfig::getExp);
        var config = getOneByWrapper(wrapper);
        // 设置最高级的信息
        if (config == null) {
            return ofVo(LevelModel.MAX_LEVEL_CONFIG);
        }
        return ofVo(config);
    }

    /**
     * 根据经验用户获取上一级的信息
     *
     * @param exp 经验数量
     * @return 上一级的信息
     */
    @Override
    public LevelConfigVo prev(Integer exp) {
        var wrapper = new LambdaQueryWrapper<LevelConfig>().le(LevelConfig::getExp, exp).orderByDesc(LevelConfig::getExp);
        var config = getOneByWrapper(wrapper);
        // 设置最低级的信息
        if (config == null) {
            return ofVo(LevelModel.MIN_LEVEL_CONFIG);
        }
        return ofVo(config);
    }

    /**
     * 根据经验用户获取当前级别的信息
     *
     * @param exp 经验数量
     * @return 当前级别的信息
     */
    @Override
    public Long level(Integer exp) {
        var wrapper = new LambdaQueryWrapper<LevelConfig>().le(LevelConfig::getExp, exp);
        return countByWrapper(wrapper) + 1;
    }

}
